package com.gskj.statstics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.util.JSONWrappedObject;
import com.gskj.commonutils.ResultJson;
import com.gskj.statstics.client.UcenterClient;
import com.gskj.statstics.entity.StatisticsDaily;
import com.gskj.statstics.mapper.StatisticsDailyMapper;
import com.gskj.statstics.service.IStatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author 康纳-郭
 * @since 2020-09-10
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements IStatisticsDailyService {

    //远程服务调用
    @Autowired
    private UcenterClient UcenterClient;

    @Override
    public void registerCount(String day) {

        // 在添加记录之前，像删除原有数据
        QueryWrapper<StatisticsDaily> q = new QueryWrapper<>();
        q.eq("date_calculated", day);
        baseMapper.delete(q);

        // 调用远程 ucenter接口 ，返回R
        ResultJson json = UcenterClient.countRegister(day);
        // 某一个的注册人数值
        Integer count = (Integer) json.getData().get("register");

        StatisticsDaily daily = new StatisticsDaily();
        // 注册人数
        daily.setRegisterNum(count);
        // 统计日期
        daily.setDateCalculated(day);

        // 自定义 随机数
        daily.setVideoViewNum(RandomUtils.nextInt(100, 201));
        daily.setCourseNum(RandomUtils.nextInt(221, 401));
        daily.setLoginNum(RandomUtils.nextInt(300, 421));

        // 将数据添加到 统计分析表
        baseMapper.insert(daily);
    }

    @Override
    public Map<String, Object> findData(String type, String begin, String end) {
        // 创建 查询条件
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();

        // 根据 统计日期 date_calculated： 查询 在 being -- end 之间
        wrapper.between("date_calculated", begin, end);

        // 指定查询的列名
        wrapper.select("date_calculated", type);
        List<StatisticsDaily> list = baseMapper.selectList(wrapper);

        //返回的有 日期、日期对应的数量
        // 封装成 map

        // 日期集合
        List<String> dateList = new ArrayList<>();
        // 数量集合
        List<Integer> countlist = new ArrayList<>();

        list.forEach(s -> {
            // 封装日期
            dateList.add(s.getDateCalculated());
            // 封装数量
            switch (type) {
                // 注册人数
                case "register_num":
                    countlist.add(s.getRegisterNum());
                    break;
                // 登录人数
                case "login_num":
                    countlist.add(s.getLoginNum());
                    break;
                // 每日播放视频数
                case "video_view_num":
                    countlist.add(s.getVideoViewNum());
                    break;
                // 每日新增课程数
                case "course_num":
                    countlist.add(s.getCourseNum());
                    break;
                default:
                    break;
            }
        });

        // 封装进 map中
        Map<String, Object> map = new HashMap<>();
        map.put("dateList", dateList);
        map.put("countList", countlist);
        return map;
    }
}
